package com.crm.system.service.impl.knowledgeitem;

import com.beust.jcommander.internal.Lists;
import com.crm.common.exception.BizBaseException;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.common.util.DateUtil;
import com.crm.model.entity.knowledgeitem.KnowledgeItem;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.knowledgeitem.KnowledgeItemQO;
import com.crm.service.knowledgeitem.KnowledgeItemDbService;
import com.crm.service.knowledgeitem.KnowledgeSectionDbService;
import com.crm.system.service.knowledgeitem.KnowledgeItemService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 知识条目
 * @Author lixq-f
 * @Date 2021/5/7
 * @Version V1.0
 **/
@Service
public class KnowledgeItemServiceImpl implements KnowledgeItemService {

    @Resource
    private KnowledgeItemDbService itemDbService;
    @Resource
    private KnowledgeSectionDbService sectionDbService;

    @Override
    public String insert(KnowledgeItemQO itemQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        KnowledgeItem kItem = new KnowledgeItem();
        kItem.setKnowledgeItemId(CommonUtils.getGUID());
        kItem.setKnowledgeItemName(itemQO.getKnItemName());
        kItem.setCreateTm(DateUtil.getSysDate());
        kItem.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        kItem.setCreatePeopleId(userAccount.getAccountId());
        Boolean addFlag = itemDbService.insert(kItem);
        return addFlag ? kItem.getKnowledgeItemId() : null;
    }

    @Override
    public List<KnowledgeItem> selectList() {
        List<KnowledgeItem> itemList = itemDbService.selectList();
        if (CollectionUtils.isEmpty(itemList))
            return Lists.newArrayList();

        return itemList;
    }

    @Override
    public Boolean update(KnowledgeItemQO itemQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        KnowledgeItem kItem = new KnowledgeItem();
        kItem.setKnowledgeItemId(itemQO.getKnItemId());
        kItem.setKnowledgeItemName(itemQO.getKnItemName());
        kItem.setUpdatePeopleId(userAccount.getAccountId());
        kItem.setUpdateTm(DateUtil.getSysDate());
        return itemDbService.update(kItem);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public Boolean deleteById(String itemId, short deleteStatus) {
        if(itemDbService.selectItemCount() == 1)
            throw new BizBaseException("The last knowledge item cannot be deleted!");
        sectionDbService.deleteByItemId(itemId, deleteStatus);
        return itemDbService.deleteById(itemId, deleteStatus);
    }


    @Override
    public KnowledgeItem selectKnItemByKey(String itemId) {
        return itemDbService.selectKnItemByKey(itemId);
    }


}
